from datetime import datetime, timedelta
from math import radians, sin, cos, sqrt, atan2
from utils.regions import EntityRegion

EARTH_RADIUS_KM = 6371


def get_current_date() -> datetime:
    return datetime.utcnow() + timedelta(hours=3)


def calculate_distance(region1: EntityRegion, region2: EntityRegion) -> int:
    return calculate_distance_points(
            region1.lat, region1.lon, region2.lat, region2.lon)


def calculate_distance_points(
        lat1: float, lon1: float, lat2: float, lon2: float) -> int:
    lat1_rad = radians(lat1)
    lon1_rad = radians(lon1)
    lat2_rad = radians(lat2)
    lon2_rad = radians(lon2)

    dlat = lat2_rad - lat1_rad
    dlon = lon2_rad - lon1_rad

    a = sin(dlat / 2)**2 + cos(lat1_rad) * cos(lat2_rad) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    return int(EARTH_RADIUS_KM * c)
